package action;

import org.apache.log4j.Logger;

import web.action.Action;

import common.LogCategory;

import agent.UserAgent;

@SuppressWarnings("serial")
public class LoginAction extends Action {

	static final Logger log = LogCategory.getCategory(LoginAction.class);

	private static final int OK = 0;

	private String username = null;
	private String password = null;

	public String getUsername() {
		return username;
	}
	public void setUsername(String value) {
		username = value;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String value) {
		password = value;
	}

	public String execute() throws Exception {
		log.info("Login Action Called");
		int result = UserAgent.getSole().checkUser(getUsername(), getPassword());
		switch (result) {
			case OK: {
				setLoggedUser(username);
				return SUCCESS;
			}
			default: {
				addActionError(getText("Login.invalidLogin"));
				return ERROR;
			} 
		}
	}

	public String logout() throws Exception {
		setLoggedUser(null);
		return SUCCESS;
	}
}